/**
 * 切片服务扩展
 */
(function(L){
    if (!L){
        console.error('Please introduce the leaflet package');
        return false;
    }

    L.TileLayer.TileServiceProvider = L.TileLayer.extend({

        initialize: function(type, options = {}) { // (type, Object)
            let providers = L.TileLayer.TileServiceProvider.providers;

            let parts = type.split('.');

            let providerName = parts[0];
            let mapName = parts[1];
            let mapType = parts[2];

            let url = providers[providerName][mapName][mapType];
            options.subdomains = providers[providerName].Subdomains;

            L.TileLayer.prototype.initialize.call(this, url, options);
        }
    });

    // Annotion: 指是路网图
    L.TileLayer.TileServiceProvider.providers = {
        TianDiTu: {
            Normal: {
                Map: 'http://t{s}.tianditu.cn/DataServer?T=vec_w&X={x}&Y={y}&L={z}',
                Annotion: 'http://t{s}.tianditu.cn/DataServer?T=cva_w&X={x}&Y={y}&L={z}',
            },
            Satellite: {
                Map: 'http://t{s}.tianditu.cn/DataServer?T=img_w&X={x}&Y={y}&L={z}',
                Annotion: 'http://t{s}.tianditu.cn/DataServer?T=cia_w&X={x}&Y={y}&L={z}',
            },
            Terrain: {
                Map: 'http://t{s}.tianditu.cn/DataServer?T=ter_w&X={x}&Y={y}&L={z}',
                Annotion: 'http://t{s}.tianditu.cn/DataServer?T=cta_w&X={x}&Y={y}&L={z}',
            },
            Subdomains: ['0', '1', '2', '3', '4', '5', '6', '7']
        },

        // 高德地图: https://lbs.amap.com/api/javascript-api/reference/layer
        GaoDe: {
            Normal: {
                Map: 'http://webrd0{s}.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=8&x={x}&y={y}&z={z}',
            },
            Satellite: {
                Map: 'http://webst0{s}.is.autonavi.com/appmaptile?style=6&x={x}&y={y}&z={z}',
                Annotion: 'http://webst0{s}.is.autonavi.com/appmaptile?style=8&x={x}&y={y}&z={z}'
            },

            Subdomains: ['1']
        },

        // 谷歌: http://www.google.cn/maps/
        Google: {
            Normal: {
                Map: 'http://www.google.cn/maps/vt?lyrs=m@189&gl=cn&x={x}&y={y}&z={z}'
            },
            Satellite: {
                Map: 'http://www.google.cn/maps/vt?lyrs=s@189&gl=cn&x={x}&y={y}&z={z}'
            },
            Terrain: {
                Map: 'http://www.google.cn/maps/vt?lyrs=m@189&gl=cn&x={x}&y={y}&z={z}&s=Galil',
            },
            Subdomains: []
        },

        // 智图: http://www.geoq.cn/index.html
        Geoq: {
            Normal: {
                Map: 'http://map.geoq.cn/ArcGIS/rest/services/ChinaOnlineCommunity/MapServer/tile/{z}/{y}/{x}',
                Color: 'http://map.geoq.cn/ArcGIS/rest/services/ChinaOnlineStreetColor/MapServer/tile/{z}/{y}/{x}',
                PurplishBlue: 'http://map.geoq.cn/ArcGIS/rest/services/ChinaOnlineStreetPurplishBlue/MapServer/tile/{z}/{y}/{x}',
                Gray: 'http://map.geoq.cn/ArcGIS/rest/services/ChinaOnlineStreetGray/MapServer/tile/{z}/{y}/{x}',
                Warm: 'http://map.geoq.cn/ArcGIS/rest/services/ChinaOnlineStreetWarm/MapServer/tile/{z}/{y}/{x}',
                Cold: 'http://map.geoq.cn/ArcGIS/rest/services/ChinaOnlineStreetCold/MapServer/tile/{z}/{y}/{x}'
            },
            Subdomains: []
        }
    };

    L.tileLayer.tileServiceProvider = function(type, options = {}) {
        return new L.TileLayer.TileServiceProvider(type, options);
    };

})(L);

